package com.yanqu.road.server.cmd.activity.entername;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityMgr;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ENTER_NAME_USER_RANK, desc = "个人排行")
public class EnterNameUserRankCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        EnterNameProto.EnterNameUserRankReqMsg reqMsg = EnterNameProto.EnterNameUserRankReqMsg.parseFrom(packet.getBytes());
        if(!EnterNameActivityMgr.activityInShowTime(reqMsg.getActivityId())){
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        if(reqMsg.getPageSize() > 1000 || reqMsg.getPage() <= 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        EnterNameProto.ChannelEnterNameUserRankReqMsg.Builder channelReq = EnterNameProto.ChannelEnterNameUserRankReqMsg.newBuilder();
        channelReq.setActivityId(reqMsg.getActivityId());
        channelReq.setUserKey(EnterNameActivityMgr.buildKeyMsg(player.getUserId()));
        channelReq.setPage(reqMsg.getPage());
        channelReq.setPageSize(reqMsg.getPageSize());
        if(reqMsg.hasType()){
            channelReq.setType(reqMsg.getType());
        }
        if(reqMsg.hasNoData()){
            channelReq.setNoData(reqMsg.getNoData());
        }
        if(reqMsg.hasUserId()){
            channelReq.setUserId(reqMsg.getUserId());
        }
        player.sendChannelPacket(ChannelProtocol.N_ENTER_NAME_USER_RANK, channelReq);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_ENTER_NAME_USER_RANK, EnterNameProto.EnterNameUserRankRespMsg.newBuilder().setRet(ret));
        }
    }
}
